package com.eorchis.core.servicetemplate.treetemplate;

import com.eorchis.core.basedao.condition.BaseTreeCondition;
import com.eorchis.core.log4j.ILog4jManager;
import com.eorchis.core.servicetemplate.treetemplate.jsonbean.JsonTreeBean;
import com.eorchis.utils.utils.PropertyUtil;
import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;

public abstract class TreeTemplate {

	@Autowired
	@Qualifier("com.eorchis.core.log4j.impl.Log4jManager")
	private ILog4jManager log4jManager;

	public final List<JsonTreeBean> process(BaseTreeCondition condition) throws Exception {
//		if (!PropertyUtil.objectNotEmpty(condition.getNode())) {
//			condition.setNode("-2");
//		}

		List treeNodeList = findNextNodeList(condition);

		condition.getParameterList().clear();

		List jsonTreeNodeList = objListToJsonList(treeNodeList);
		if ((jsonTreeNodeList != null) && (!jsonTreeNodeList.isEmpty())) {
			buildJsonTreeBeanByList(jsonTreeNodeList, condition);
		} else
			this.log4jManager.errorCustomLog(getClass().getName(), "process", "查询出来的下级节点集合为空");

		return jsonTreeNodeList;
	}
	
	public final List<JsonTreeBean> process1(BaseTreeCondition condition) throws Exception {
		if (!PropertyUtil.objectNotEmpty(condition.getNode())) {
			condition.setNode("-2");
		}
		
		List treeNodeList = findNextNodeList(condition);
		
		condition.getParameterList().clear();
		
		List jsonTreeNodeList = objListToJsonList(treeNodeList);
		if ((jsonTreeNodeList != null) && (!jsonTreeNodeList.isEmpty())) {
			buildJsonTreeBeanByList(jsonTreeNodeList, condition);
		} else
			this.log4jManager.errorCustomLog(getClass().getName(), "process", "查询出来的下级节点集合为空");
		
		return jsonTreeNodeList;
	}

	private void buildJsonTreeBeanByList(List<JsonTreeBean> treeNodeList, BaseTreeCondition condition) throws Exception {
		if ("select_tree".equals(condition.getTreeType())) {
			if ((condition.getRootID() != null) && (!"".equals(condition.getRootID()))) {
				JsonTreeBean json = null;

				for (JsonTreeBean jtb : treeNodeList) {
					if (condition.getRootID().equals(jtb.getId())) {
						json = buildChildList(treeNodeList, jtb, condition);
						json.setLeaf(true);
						json.setState("open");
						break;
					}
				}
				treeNodeList.clear();
				treeNodeList.add(json);
			} else {
				for (JsonTreeBean jtb : treeNodeList) {
					if ((condition.getDefaultCheckedValue() != null) && (!"".equals(condition.getDefaultCheckedValue()))) {
						jtb.setChecked(findCheckedState(condition, jtb));
						jtb.setLeaf(false);
						jtb.setState("closed");
					}
				}
			}

		} else if (condition.isFindLeapNode()) {
			for (JsonTreeBean jtb : treeNodeList) {
				condition.setNode(jtb.getId());

				if (isLeapNode(condition)) {
					jtb.setLeaf(true);
					jtb.setState("open");
					jtb.setExpanded(Boolean.valueOf(true));
				} else {
					jtb.setLeaf(false);
					jtb.setState("closed");
				}

				jtb.setExpandable(Boolean.valueOf(true));
			}
		}
	}

	private JsonTreeBean buildChildList(List<JsonTreeBean> treeNodeList, JsonTreeBean eachBean, BaseTreeCondition condition) throws Exception {
		condition.setNode(eachBean.getId());
		if (condition.isFindLeapNode()) {
			if (isLeapNode(condition)) {
				eachBean.setLeaf(true);
				eachBean.setState("open");
			} else {
				eachBean.setLeaf(false);
				eachBean.setState("closed");
			}
		}

		eachBean.setChecked(findCheckedState(condition, eachBean));
		if (eachBean.isLeaf()) {
			List result = new ArrayList();
			for (JsonTreeBean jtb : treeNodeList) {
				if ((jtb.getParentID() != null) && (!"".equals(jtb.getParentID())) && (eachBean.getId().equals(jtb.getParentID()))) {
					result.add(buildChildList(treeNodeList, jtb, condition));
				}
			}

			if (!result.isEmpty()) {
				eachBean.setChildren(result);
			}
		}
		return eachBean;
	}

	protected abstract List<?> findNextNodeList(BaseTreeCondition paramBaseTreeCondition) throws Exception;

	protected abstract boolean isLeapNode(BaseTreeCondition paramBaseTreeCondition) throws Exception;

	protected abstract List<JsonTreeBean> objListToJsonList(List<?> paramList) throws Exception;

	private Boolean findCheckedState(BaseTreeCondition condition, JsonTreeBean eachBean) throws Exception {
		Boolean result = Boolean.valueOf(false);
		if ((condition.getDefaultCheckedValue() != null) && (!"".equals(condition.getDefaultCheckedValue()))) {
			if (condition.getDefaultCheckedValue().indexOf("," + eachBean.getId() + ",") != -1) {
				result = Boolean.valueOf(true);
			}
		} else
			this.log4jManager.errorCustomLog(getClass().getName(), "findCheckedState", "默认选中值为空");

		return result;
	}
}